var RANGS = [1000, 10000, 100000, 1000000];
var DEGREE = 240;

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    onLoad() {
        this.range = this.node.find('oil_range', cc.Sprite);
        this.pointer = this.node.find('oil_pointer');
        this.text = this.node.find('txt_count', cc.Label);
        this.textAdd = this.node.find('txt_add', cc.RichText);

        this.count = 0;
        this.clearBet();
    },

    clearBet(){
        this.count = 0;
        this.range.fillRange = 0;
        this.pointer.rotation = -120;
        this.text.string = '0';
    },

    modify(count) {
        this.count += count;
        this.text.string = this.count;
        var degree = 0;
        var perDegree = DEGREE / RANGS.length;
        if(this.count < RANGS[0]) {
            degree = this.count / RANGS[0] * perDegree;
        } else if (this.count < RANGS[RANGS.length - 1]) {
             for(var i = 1; i < RANGS.length; i++) {
                if(this.count >= RANGS[i - 1] && this.count < RANGS[i]) {
                    degree = perDegree * i + (this.count - RANGS[i - 1]) / (RANGS[i] - RANGS[i - 1]) * perDegree;
                    break;
                }
            }
        } else {
           degree = DEGREE;
        }
        this.pointer.rotation = -DEGREE / 2 + degree;
        this.range.fillRange = -(degree / DEGREE) * 0.66;
        this.floatText(count);
    },

    floatText(count) {
        var ftext = cc.instantiate(this.textAdd.node);
        ftext.getComponent(cc.RichText).string = '<outline color=black width=2>+' + count + '</outline>';
        ftext.parent = this.node;
        ftext.runAction(cc.moveBy(1, 0, 100));
        ftext.runAction(cc.sequence(
            cc.fadeIn(0.1),
            cc.delayTime(0.6),
            cc.fadeOut(0.3),
            cc.callFunc(() => ftext.parent = null, this)));
    }

});
